<template>
  <a-card>
    <a-form>
      <a-row>
        <a-col :span="24">
          <a-form-item label="包含英文" :label-col="{span: 8}" :wrapper-col="{span: 16}">
            <el-checkbox v-model="upwer">大写字母</el-checkbox>
            <el-checkbox v-model="lower">小写字母</el-checkbox>
          </a-form-item>
          <a-form-item label="包含数字" :label-col="{span: 8}" :wrapper-col="{span: 16}">
            <el-switch
              v-model="containNumber"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </a-form-item>
          <a-form-item label="特殊字符" :label-col="{span: 8}" :wrapper-col="{span: 16}">
            <el-switch
              v-model="specialChar"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </a-form-item>
          <a-form-item label="密码长度" :label-col="{span: 8}" :wrapper-col="{span: 16}">
            <el-input-number size="mini" v-model="passwdLen" :min="1" :max="100"/>
          </a-form-item>
        </a-col>
        <a-col :span="24" style="text-align: center">
          <a-button @click="buildPasswd" style="width: 50%;">生成密码</a-button>
        </a-col>
        <a-col :span="24" style="text-align: center" v-if="passwd != ''">
          <el-input placeholder="请输入内容" v-model="passwd" style="width: 70%;margin-top: 20px">
            <el-button slot="append" type="primary" icon="el-icon-document-copy" @click="copyPass" :disabled="passwd == ''">
              复制
            </el-button>
          </el-input>
        </a-col>
      </a-row>
    </a-form>

  </a-card>
</template>


<script>
import {getAction, getFileAccessHttpUrl, postAction} from '@/api/manage'
import AntsImageUpload from '@/components/ants/AntsImageUpload'

export default {
  name: 'docToPdf',
  components: {
    AntsImageUpload
  },
  data() {
    return {
      titles: "密码生成器",
      passwdLen: 12,
      passwd: '',
      upwer: true,
      lower: true,
      containNumber: true,
      specialChar: true,
    }
  },
  created() {
  },
  methods: {
    buildPasswd() {
      let passwdArray = [];
      let randPass = "";
      // 数字
      if (this.containNumber) {
        for (let id = 48; id < 58; id++) {
          passwdArray.push(String.fromCharCode(id))
        }
      }
      // 小写字母
      if (this.lower) {
        for (let id = 97; id < 123; id++) {
          passwdArray.push(String.fromCharCode(id))
        }
      }
      // 大写字母
      if (this.upwer) {
        for (let id = 65; id < 91; id++) {
          passwdArray.push(String.fromCharCode(id))
        }
      }
      // 特殊字符
      if (this.specialChar) {
        for (let id = 33; id < 47; id++) {
          passwdArray.push(String.fromCharCode(id))
        }
      }
      // 检查是否什么都没选
      if (passwdArray.length == 0) {
        return
      }
      // 生成密码
      for (let id = 0; id < this.passwdLen; id++) {
        let n = Math.floor(Math.random() * passwdArray.length)
        randPass = randPass.concat(passwdArray[n])
      }
      // 密码
      this.passwd = randPass
    },
    copyPass() {
      navigator.clipboard.writeText(this.passwd)
        .then(() => {
          this.$message.success('复制成功')
        })
        .catch(() => {
          this.$message.error('复制失败')
        })
    },
  }

}
</script>

<style>
.spin-content {
  border: 1px solid #91d5ff;
  background-color: #e6f7ff;
  padding: 30px;
  margin-top: 10px;
}

</style>

